<?php
header("Content-type:text/html;charset=utf-8");
set_time_limit(0);
$data=$_POST;
$tmpTime=explode('至',$_POST['time']);
$strTime=strtotime($tmpTime[0]);
$entTime=strtotime($tmpTime[1]);
$searchtime=$_POST['searchtime'];
switch ($searchtime){
    case 'create':
        $searchtime='createtime';
        break;
    case 'pay':
        $searchtime='paytime';
        break;
    case 'send':
        $searchtime='sendtime';
        break;
    case 'finish':
        $searchtime='finishtime';
        break;
    default:
        $searchtime='createtime';
}
$mysqli=new mysqli('localhost','we7shop','we7shop','we7shop');
$str=$strTime.' and '.$entTime;

$sql="SELECT d.id,e.groupname,f.name,c.title,c.shorttitle,c.goodssn,c.unit,c.marketprice,b.total FROM `ims_ewei_shop_order` as a INNER JOIN `ims_ewei_shop_order_goods` as b on a.id=b.orderid INNER join `ims_ewei_shop_goods` as c on b.goodsid=c.id inner join `ims_ewei_shop_member` as d on a.openid=d.openid left join `ims_ewei_shop_member_group` as e on d.groupid=e.id inner join `ims_ewei_shop_category` as f on c.ccate=f.id WHERE c.uniacid=16 and a.$searchtime BETWEEN $str ORDER BY a.id desc";

if ($result = $mysqli->query($sql)) {
    $arr=[];

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        $arr[]=$row;
    }
}


$mysqli->close();
$name=time();
$index=array('id','groupname','name','title','shorttitle','goodssn','unit','marketprice','total');
echo toExcel($arr,$name,$index);
function toExcel($list,$filename,$indexKey,$startRow=1,$excel2007=false){
    //文件引入
    // echo $pe['host_root'].'include/class/PHPExcel.php';die;
    include('./PHPExcel/Classes/PHPExcel.php');
    include('./PHPExcel/Classes/PHPExcel/Writer/Excel2007.php');
    // require("/include/class/PHPExcel.php");
    // include("/include/class/PHPExcel/Writer/Excel2007.php");
    ob_end_clean();
    if(empty($filename)) $filename = time();
    if( !is_array($indexKey)) return false;

    $header_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
    //初始化PHPExcel()
    $objPHPExcel = new PHPExcel();

    //设置保存版本格式
    if($excel2007){
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        $filename = $filename.'.xlsx';
    }else{
        $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
        $filename = $filename.'.xls';
    }

    //接下来就是写数据到表格里面去
    $objActSheet = $objPHPExcel->getActiveSheet();
    //$startRow = 1;
    foreach ($list as $row) {
        foreach ($indexKey as $key => $value){
            //这里是设置单元格的内容
            $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);
        }
        $startRow++;
    }

    // 下载这个表格，在浏览器输出

    $objWriter->save("./excel/$filename");
    return "/excel/$filename";
}
